'''
Created on 17.7.2011

@author: gilead
'''

from panda3d.core import Vec4 #@UnresolvedImport
from panda3d.core import LineSegs #@UnresolvedImport

class SingleLine(object):
    '''
    A simple 3D line that knows how to redraw, remove, hide and show
    '''


    def __init__(self, parent, thickness = 2.0, color = Vec4(0, 0, 0, 1)):
        '''
        Constructor
        '''
        self.segs = LineSegs()
        self.segs.setThickness( thickness ) 
        self.segs.setColor( color )
        
        self.oldLine = None
        self.parent = parent
        
    # returns true iff the line was removed
    def remove(self):
        if self.oldLine == None:
            return False
        self.parent.node().removeChild(self.old)
        self.oldLine = None
        return True
    
    def hide(self):
        if self.oldLine == None:
            return False
        self.parent.node().removeChild(self.oldLine)
        return True
    
    def show(self):
        if self.oldLine != None:
            self.parent.attachNewNode(self.oldLine)
    
    def redraw(self, a, b, c, x, y, z):
        self.segs.moveTo(a,b,c)
        self.segs.drawTo(x,y,z)
        new = self.segs.create()
        if self.oldLine != None:
            self.parent.node().replaceChild(self.oldLine, new)
        else:
            self.parent.attachNewNode(new)
        self.oldLine = new
        
    def redrawvectors(self, a, b):
        self.redraw(a.getX(), a.getY(), a.getZ(), b.getX(), b.getY(), b.getZ())